package fetchWeb;

import java.net.*;
import java.io.*;

public class ReaderWeb {

	BufferedReader br; 
	FileWriter fr;
	URL webPage; 
	
    public ReaderWeb(String url, String fn) throws Exception {
    	webPage = new URL(url); 
    	fr = new FileWriter(fn); 
	}
	
	public void toSystemout() throws Exception{
	        URLConnection urlc = webPage.openConnection();
	        br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
	        String inputLine;
	        while ((inputLine = br.readLine()) != null) 
	            System.out.println(inputLine);
	        br.close();
	    }

	public void toFile() throws Exception {
    URLConnection urlc = webPage.openConnection();
    InputStreamReader ir = new InputStreamReader(urlc.getInputStream());
    int c; 
    int linelen = 80; 
    int linecnt = 0; 
    while ((c = ir.read()) != -1) { 
    	fr.write(c); 
    	linecnt++;  
    	if (linecnt > linelen && c == '>') { 
    		fr.write('\n'); 
    		linecnt = 0; 
    	}
    }
    br.close();
}

	public static void main(String[] args) throws Exception {
		ReaderWeb rw = new ReaderWeb(args[0], args[1]); 
		rw.toSystemout();
		rw.toFile(); 
		System.out.println("\nDone");
	}

}
